package com.sky.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {


    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加菜品
     * @param dishDTO
     */
    public void addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);
//        for(DishFlavor dishFlavor: dishDTO.getFlavors()){
//            dishFlavor.setDishId(dish.getId());
//            dishFlavorMapper.insertDishFlavor(dishFlavor);
//        }
        List<DishFlavor> flavorList = dishDTO.getFlavors();
        flavorList.forEach(dishFlavor ->{
            dishFlavor.setDishId(dish.getId());
        });
        dishFlavorMapper.insertDishFlavor(flavorList);
    }

    /**
     * 分页查询
     * @param dishPageQueryDTO
     * @return
     */
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        List<DishVO> dishVOList = dishMapper.page(dishPageQueryDTO);
        Page<DishVO> dishPage = (Page<DishVO>) dishVOList;
        return new PageResult(dishPage.getTotal(), dishPage.getResult());
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @Transactional
    public void deleteDish(List<Long> ids) {

        //1.菜品处于起售状态的话不能删除
        ids.forEach((id) ->{
            Dish dish = dishMapper.getById(id);
            if(dish.getStatus() == 1){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });

        //2.菜品关联了套餐 不能删除
        if(setmealDishMapper.getByDishId(ids).size() >0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        //3.删除菜品及菜品下的口味做法
            dishMapper.deleteDishByid(ids);
            dishFlavorMapper.deleteByDishId(ids);

    }

    /**
     * 菜品回显
     * @param id
     * @return
     */
    public DishVO getDishByID(Long id) {
        Dish dish = dishMapper.getById(id);
        Long categoryId = dishMapper.getCategoryIdByID(id);
        List<DishFlavor> dishFlavors = dishMapper.getDishFlavorByDishID(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavors);
        dishVO.setCategoryId(categoryId);
        return dishVO;
    }

    /**
     * 菜品修改
     * @param dishDTO
     * @return
     */
    public void updateDish(DishDTO dishDTO) {

        //1.更新菜品表
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.updateDish(dish);

        //2.更新口味表
        List<DishFlavor> dishFlavorList = dishDTO.getFlavors();
        dishFlavorList.forEach((dishFlavor -> {
            dishFlavor.setDishId(dish.getId());
        }));

        List<Long> dish_ids = new ArrayList<>();
        dish_ids.add(dish.getId());
        List<DishFlavor> dishFlavors = dishMapper.getDishFlavorByDishID(dish.getId());
        if (dishFlavors != null && !dishFlavors.isEmpty()) {
            dishFlavorMapper.deleteByDishId(dish_ids);
            dishFlavorMapper.insertDishFlavor(dishFlavorList);
        }
    }

    /**
     * 菜品起售、停售
     * @param id
     * @param status
     */
    public void startOrStop(Long id, Integer status) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();

        dishMapper.updateDish(dish);

        //如果是停售操作，那么菜品所关联的套餐也不能售卖
        if(status == StatusConstant.DISABLE){
            ArrayList<Long> dishIds = new ArrayList<>();
            dishIds.add(id);
            // select setmealId from setmeal_dish where dish_id in (?,?,?)
            List<Long> setmealIds = setmealDishMapper.getByDishId(dishIds);
            if(setmealIds != null &&setmealIds.size()>0){
                for (Long setmealId :setmealIds) {
                    Setmeal setmeal = Setmeal.builder()
                            .id(setmealId)
                            .status(StatusConstant.DISABLE)
                            .build();
                    setmealMapper.update(setmeal);
                }
            }
        }
    }


    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);


        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishMapper.getDishFlavorByDishID(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }


    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> list(Long categoryId) {
        Dish dish=Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }
}
